﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MEMDB
{
    public partial class CreateTable : Form
    {
        SQLConnection m_conn;
        bool isSuccessful = false;

        public string msg = "";

        public CreateTable(SQLConnection conn)
        {
            InitializeComponent();
            m_conn = conn;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ColumnNametextBox1_TextChanged(object sender, EventArgs e)
        {
            if (ColumnNametextBox1.Text == "")
            {
                AddColumnbutton1.Enabled = false;
            }
            else if(columnTypecomboBox1.Text != "")
                AddColumnbutton1.Enabled = true;
        }

        private void columnTypecomboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(((ComboBox)sender).SelectedIndex == 2)
            {
                StringLengthnumericUpDown1.Visible = true;
            }
            else
            {
                StringLengthnumericUpDown1.Visible = false;
            }
        }

        private void columnTypecomboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            if (columnTypecomboBox1.Text == "")
            {
                CreateTableButton.Enabled = false;
                AddColumnbutton1.Enabled = false;
            }
            else if (ColumnNametextBox1.Text != "")
                AddColumnbutton1.Enabled = true;
        }

        private void columnTypecomboBox1_TextChanged(object sender, EventArgs e)
        {
            if (columnTypecomboBox1.SelectedText != "varchar2")
                StringLengthnumericUpDown1.Visible = false;
            else
                StringLengthnumericUpDown1.Visible = true;
        }

        private void DefaultValuecheckBox1_Click(object sender, EventArgs e)
        {
            if (DefaultValuecheckBox1.Checked == false)
                DefaultValuetextBox1.Visible = false;
            else
                DefaultValuetextBox1.Visible = true;
        }

        private void RemoveColumnbutton1_Click(object sender, EventArgs e)
        {
            listView1.Items.Remove(listView1.SelectedItems[0]);
            if (listView1.Items.Count == 0)
            {
                CreateTableButton.Enabled = false;
                RemoveColumnbutton1.Enabled = false;
            }
        }

        private void AddColumnbutton1_Click(object sender, EventArgs e)
        {
            string column = ColumnNametextBox1.Text.Trim() + " " + columnTypecomboBox1.Text.Trim();
            
            if(columnTypecomboBox1.Text == "varchar2")
            {
                column += "(" + StringLengthnumericUpDown1.Value.ToString() + ")";
            }

            if (IsPrimaryKeycheckBox1.Checked)
                column += " primary key";

            if (NotNullcheckBox1.Checked)
                column += " not null";

            if (DefaultValuecheckBox1.Checked)
            {
                column += " default ";

                if (columnTypecomboBox1.Text == "varchar2")
                {
                    column += "'" + DefaultValuetextBox1.Text + "'";
                }
                else
                    column += DefaultValuetextBox1.Text;
            }

            listView1.Items.Add(column);

            if(TableNametextBox1.Text != "")
                CreateTableButton.Enabled = true;
        }

        private void CreateTableButton_Click(object sender, EventArgs e)
        {
            SQLCommand cmd = new SQLCommand("", m_conn);
            cmd.m_commandString = "create table " + TableNametextBox1.Text + "(";

            for(int i = 0; i < listView1.Items.Count; i++)
            {
                cmd.m_commandString += listView1.Items[i].Text;
                if (i != listView1.Items.Count - 1)
                    cmd.m_commandString += ",";
            }

            cmd.m_commandString += ");";

            try
            {
                int iRet = cmd.ExecuteNonQuery();
                this.msg = cmd.getRetMsg();
                if (iRet == -1)
                {
                    MessageBox.Show("创建表失败。原因请见下方输出窗口。");
                    this.Close();
                    return;
                }
            }
            catch(SQLException ex)
            {
                MessageBox.Show("连接已关闭。\r\n" + ex.Message);
            }
            isSuccessful = true;
            this.Close();
            //System.Windows.Forms.MessageBox.Show("创建表成功！");
        }

        public String getMsg()
        {
            return msg;
        }

        public bool Successful
        {
            get
            {
                return isSuccessful;
            }
        }

        private void TableNametextBox1_TextChanged(object sender, EventArgs e)
        {
            if (TableNametextBox1.Text == "")
                CreateTableButton.Enabled = false;
            else if(listView1.Items.Count != 0)
                CreateTableButton.Enabled = true;
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count != 0)
                RemoveColumnbutton1.Enabled = true;
            else
                RemoveColumnbutton1.Enabled = false;
        }
    }
}
